package cards;

import java.util.Collection;
import java.util.Scanner;

/**
 * Text user interface for the game Prsi
 * @author pc00088
 */
public class UIText {
  
    //card game, that would this class use
    private CardGameIface game;
    
    private final String DELIMITER = "-----------------------------------------%n";
    
    public UIText(CardGameIface cardGame) {
        this.game = cardGame;
    }
    
    /**
     * Loops until game is over
     */
    public void play() {
        Player playerOnMove;
        GameState gameState;  
        do {
            displayDiscardPile();
            playerOnMove = game.getPlayerOnMove();
            displayHand(playerOnMove);
            gameState = game.turn(enterCardIndex());
            switch(gameState) {
                case NextDrawsTwo : {
                    //TODO - move the game logic to the class PrsiGame
                    // + implement the "over" play - draw 4 cards when there
                    // are 2 * SEDM card
                    
                }
                case StopNextPlayer : {
                    //TODO - move the game logic to the class PrsiGame
                    //!!! for it to be implemented even for the first card,
                    //  which is not played by any player
                    if (game.getPlayerOnMove().getHand().containsRank(Rank.ESO)) {
                        //Next player has ESO in his hand - he can continue
                        break;
                    } else {
                        //Next player cannot play one round
                        System.out.println("Next player waits one round.");
                        game.setNextPlayerOnMove();
                        break;
                    }
                }
                case CardNotAllowed : {
                    System.out.println("This card is not allowed. Choose another one");
                    break;
                }    
                default : break;
            }    
        } while (!gameState.equals(GameState.Finish));
        System.out.print("Game finished. The winner is " + playerOnMove.getName());
        System.out.println("Congratulations!");
    }
    
    /**
     * Displays cards in the hand of player
     * @param player whose cards are to be displayed
     */
    private void displayHand(Player player) {
        StringBuilder sb = new StringBuilder();
        sb.append(DELIMITER);
        sb.append("Player ").append(player.getName()).append(":");
        Collection<? extends Card> cardsInTheHand = player.getHand().getCardsInTheHand();
        int counter = 0;
        for(Card card : cardsInTheHand) {
            sb.append("| ").append(counter).append(" ").append(card.toString()).append("\t");
            counter++;
            if( counter % 4 == 0 ) {
                sb.append("%n");
            }
        }
        sb.append("%n");
        System.out.printf(sb.toString());
    }
    
    /**
     * Lets the player to enter index of the chosen card
     * @return int index of the card to be played
     */
    private int enterCardIndex() {
        System.out.printf(DELIMITER);       
        String message = "Enter index of the card that you want to play.%n"
                + "Enter \'-1\' to draw a card." ;
        return getIntFromStdin(message);
    }
    
    /**
     * Prints all suits and lets the player select a new suit for the game
     * @return Suit selected suit
     */
    private Suit selectNewSuit() {
        System.out.printf(DELIMITER); 
        System.out.println("Select new suit: ");
        int counter = 1;
        for(Suit suit : Suit.values()) {
            System.out.println(counter + ") " + suit.toString());
        }
        int suitIndex;
        String message = "Select the number of the new suit:";
        do {
            suitIndex = getIntFromStdin(message);
        } while (suitIndex > 4 && suitIndex < 1);
        
        return Suit.values()[suitIndex -1];                
    }
    
    /**
     * Return int that is entered to the standard input (console)
     * @param message String to be displayed
     * @return int number entered
     */
    private int getIntFromStdin(String message) {
        Scanner in = new Scanner(System.in);
        System.out.println(message);
        while(!in.hasNextInt()) {            
            in.next();
        }
        int number = in.nextInt();
        return number;
    }
    
    /**
     * Displays top card of the discard pile
     */
    private void displayDiscardPile() {
        Card topCard = game.getLastPlayedCard();
        System.out.println(DELIMITER);
        System.out.println("Last played card:");
        System.out.println("| " + game.getLastPlayedCard());
        if(topCard.getRank().equals(Rank.SVRSEK)) {
            System.out.println("Suit: " + game.getPresentSuit());
        }    
    }
    
    
}
